<script>
(function autoModalStart() {
    // 1) Modus aus dem Widget lesen
    var widget = document.querySelector('.suche-widget');
    var mode   = widget ? widget.getAttribute('data-result-mode') : 'inline';
    
    // Wenn im Admin "inline" eingestellt ist → gar nichts machen
    if (mode !== 'modal') {
        return;
    }
    
    // 2) Inline-Ergebnisse und Modal-Elemente holen
    var resultsEl   = document.getElementById('searchResults');
    var modalEl     = document.getElementById('searchModal');
    var modalBodyEl = document.getElementById('searchModalBody');
    
    if (!resultsEl || !modalEl || !modalBodyEl) {
        return;
    }
    
    // 3) Warten, bis Bootstrap 5 geladen ist
    if (typeof bootstrap === "undefined" || !bootstrap.Modal) {
        setTimeout(autoModalStart, 200);
        return;
    }
    
    // 4) Styling für hübschere Darstellung hinzufügen
    if (!document.getElementById('searchModalStyles')) {
    let css = `
        /* Modal Header Styling */
        #searchModal .modal-header {
            background: #343a40;   /* dunkles Bootstrap Grau */
            border-bottom: none;
        }
        
        #searchModal .modal-title {
            font-weight: 600;
            font-size: 1.25rem;
            color: white;
        }
        
        /* Modal Body */
        #searchModal .modal-body {
            background: #f4f5f7;   /* sehr helles Grau */
        }
        
        /* Ergebnisliste */
        #searchModalBody ul {
            list-style: none;
            padding: 0;
            margin: 0;
        }
        
        #searchModalBody li {
            background: white;
            border: 1px solid #d9d9d9;
            border-radius: 8px;
            padding: 18px;
            margin-bottom: 14px;
            transition: all 0.25s ease;
        }
        
        #searchModalBody li:hover {
            transform: translateY(-1px);
            box-shadow: 0 2px 8px rgba(0,0,0,0.08);
            border-color: #007bff;   /* dezentes Bootstrap-Blau */
        }
        
        /* Link Styling */
        #searchModalBody a {
            color: #0056b3;         /* seriöses Dunkelblau */
            text-decoration: none;
            font-weight: 600;
            font-size: 1.05rem;
            display: inline-block;
            margin-bottom: 6px;
        }
        
        #searchModalBody a:hover {
            color: #0a58ca;         /* leicht heller */
        }
        
        /* Quelle-Text */
        #searchModalBody small {
            display: block;
            color: #6c757d;
            font-size: 0.85rem;
            margin-bottom: 8px;
        }
        
        /* Preview Text */
        #searchModalBody li > div {
            color: #495057;
            line-height: 1.55;
            font-size: 0.95rem;
        }
        
        /* Modal Footer */
        #searchModal .modal-footer {
            background: #f1f1f1;
            border-top: 1px solid #d9d9d9;
        }
        
        /* Scrollbar Styling */
        #searchModal .modal-body::-webkit-scrollbar {
            width: 8px;
        }
        
        #searchModal .modal-body::-webkit-scrollbar-track {
            background: #eee;
            border-radius: 10px;
        }
        
        #searchModal .modal-body::-webkit-scrollbar-thumb {
            background: #aaa;
            border-radius: 10px;
        }
        
        #searchModal .modal-body::-webkit-scrollbar-thumb:hover {
            background: #888;
        }
        
        /* Responsive Optimierung */
        @media (max-width: 768px) {
            #searchModalBody li {
                padding: 14px;
            }
            
            #searchModalBody a {
                font-size: 1rem;
            }
        }
    `;
    
    let styleTag = document.createElement('style');
    styleTag.id = 'searchModalStyles';
    styleTag.innerHTML = css;
    document.head.appendChild(styleTag);
}

    
    // 5) Inhalt aus #searchResults in den Modal-Body kopieren
    modalBodyEl.innerHTML = resultsEl.innerHTML;
    
    // 6) Inline-Ergebnisse ausblenden
    resultsEl.style.display = 'none';
    
    // 7) Modal anzeigen
    var modal = new bootstrap.Modal(modalEl);
    modal.show();
})();
</script>
